package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyx.Common.R;
import com.wyx.Pojo.Dish;
import com.wyx.Pojo.Setmeal;
import com.wyx.Pojo.ShoppingCart;
import com.wyx.Pojo.User;
import com.wyx.Service.DishService;
import com.wyx.Service.SetmealService;
import com.wyx.Service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingcarController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //客户端获取购物车的数据
    @GetMapping("/list")
    @Transactional
    public R<List<ShoppingCart>> getlist(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
//        System.out.println(user);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, user.getId());
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        int fale = 0;
        List<ShoppingCart>res=new ArrayList<>();
        for (ShoppingCart shoppingCart : list) {
            //获取菜品的id
            Long dishId = shoppingCart.getDishId();
            //获取套餐的id
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId!=null){
                //在重新查询数据库 看看是否与数据库一致
                Dish byId = dishService.getById(dishId);
                if (!byId.getPrice().divide(BigDecimal.valueOf(100)).equals(shoppingCart.getAmount())) {
                    shoppingCart.setAmount(byId.getPrice().divide(BigDecimal.valueOf(100)));
                    fale++;
                }
                if (byId.getStatus()!=0){
                    fale++;
                    ShoppingCart newshoppingcart=new ShoppingCart();
                    BeanUtils.copyProperties(shoppingCart,newshoppingcart);
                    res.add(newshoppingcart);
                }
            }
            if (setmealId!=null){
                Setmeal setmeal = setmealService.getById(setmealId);
                if (!setmeal.getPrice().divide(BigDecimal.valueOf(100)).equals(shoppingCart.getAmount())){
                    shoppingCart.setAmount(setmeal.getPrice().divide(BigDecimal.valueOf(100)));
                    fale++;
                }
                if (setmeal.getStatus()==0){
                    fale++;
                }
                if (setmeal.getStatus()!=0){
                    ShoppingCart newshoppingcart=new ShoppingCart();
                    BeanUtils.copyProperties(shoppingCart,newshoppingcart);
                    res.add(newshoppingcart);
                }
            }


        }
        //如果标志动了 也就是说后台价格变化了 那么前端也要跟着变
        if (fale != 0) {
            //将用户的所有购物车数据删了 然后在新增
            shoppingCartService.remove(wrapper);
            shoppingCartService.saveBatch(res);
        }
        return R.success(res);
    }

    /**
     * 购物车商品增加数量
     * @param shoppingCart
     * @param request
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        //提交的是套餐
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        User user = (User) request.getSession().getAttribute("user");
        wrapper.eq(ShoppingCart::getUserId,user.getId());
        if (shoppingCart.getDishId() == null) {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            //提交的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(user.getId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            one = shoppingCart;
            shoppingCartService.save(shoppingCart);
        } else {
            one.setNumber(one.getNumber() + 1);
            one.setCreateTime(LocalDateTime.now());
            shoppingCartService.update(one, wrapper);
        }
        return R.success(one);
    }

    /**
     * 购物车里的商品减少数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart) {

        System.out.println(shoppingCart);


        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() == null) {
            //该商品是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if (one.getNumber() == 1) {
                //当商品数量为1时候减少就为0了
                shoppingCartService.removeById(one.getId());
                ShoppingCart shoppingCart1 = new ShoppingCart();
                shoppingCart1.setNumber(0);
                return R.success(shoppingCart1);
            } else {
                //当商品数量不为1时候
                one.setNumber(one.getNumber() - 1);
                shoppingCartService.updateById(one);
                return R.success(one);
            }
        } else {
            //该商品是套餐
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if (one.getNumber() == 1) {
                //当商品数量为1时候减少就为0了
                shoppingCartService.removeById(one.getId());
                ShoppingCart shoppingCart1 = new ShoppingCart();
                shoppingCart1.setNumber(0);
                return R.success(shoppingCart1);
            } else {
                //当商品数量不为1时候
                one.setNumber(one.getNumber() - 1);
                shoppingCartService.updateById(one);
                return R.success(one);
            }

        }
    }

    @DeleteMapping("/clean")
    public R<String> cleanall(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        //把当前用户的所有的购物车菜品，套餐给删了
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, user.getId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功~");
    }
}
